package com.yunsom.testdemo.multiple_thread.producer_customer.wait_notify;

import java.util.Queue;
import java.util.Random;

public class Producer extends Thread{
    private Queue<Integer> queue;
    private int maxSize;

    public Producer(Queue<Integer> queue, int maxSize, String name){
        super(name);
        this.queue = queue;
        this.maxSize = maxSize;
    }


    public void run(){
        while (true){
            synchronized(queue){
                while (queue.size() == maxSize){
                    try {
                        System.out.println("队列已满，停止生产，线程进入等待");
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                int i = new Random().nextInt();
                System.out.println("生产产品" + i + "号");
                queue.add(i);
                queue.notifyAll();
            }
        }
    }
}
